<template>
  <div id="cesiumContainer"></div>
</template>

<script setup>
import * as Cesium from 'cesium';
import {onMounted} from "vue";

onMounted(async () => {
  Cesium.Ion.defaultAccessToken = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJqdGkiOiI0NTI3OGI2Ni05YmJhLTRlMTYtYjdiNC1hM2E5ZjI1MGYxZmIiLCJpZCI6MTQ0NTcxLCJpYXQiOjE3MDMxNTQ2MzZ9.-ab5sCTIXoyCUMMGv35NQBbR0h7pxsxu78D3AfCVTek'
  const customImageryProvider = new Cesium.ArcGisMapServerImageryProvider({
    url: '//services.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer'
  })
  const mapboxImageryProvider = new Cesium.UrlTemplateImageryProvider({
    url: 'https://a.tiles.mapbox.com/v4/mapbox.satellite/{z}/{x}/{y}.png?access_token=pk.eyJ1IjoibWFwYm94ZGV2dXNlcjUyMCIsImEiOiJjbHFrb2w3M2IyNHB2MnFtZWx1ZzFjNHZmIn0.CMoLsalSeXlJiLBNyUK1nA'
  })
  var url = 'http://localhost:8081/geoserver/nurc/wms'; //Geoserver URL
  // Cesium之Viewer初始常用配置
  const viewer = new Cesium.Viewer('cesiumContainer', {
    shadows: false, // 阴影
    animation: false, // 左下角控制动画
    baseLayerPicker: false, // 右上角图层选择器
    fullscreenButton: false, // 全屏
    geocoder: false, // 右上角搜素
    homeButton: false, // 主页
    infoBox: false, // 信息框
    scene3DOnly: true, // 仅仅显示3D，可以隐藏右上角2D和3D按钮
    selectionIndicator: false, // 绿色选择框
    timeline: false, // 最下面时间轴
    navigationHelpButton: true, // 右上角帮助按钮
    navigationInstructionsInitiallyVisible: true, // 导航指令
    useDefaultRenderLoop: true,
    showRenderLoopErrors: true,
    projectionPicker: false, // 投影选择器
    terrainProvider: await Cesium.createWorldTerrainAsync(), // 地形
    // imageryProvider:  wmsImageryProvider // 影像图
  });
  viewer.scene.globe.depthTestAgainstTerrain = true; // 开启地形深度检测
  // 取消双击时间
  viewer.cesiumWidget.screenSpaceEventHandler.removeInputAction(Cesium.ScreenSpaceEventType.LEFT_DOUBLE_CLICK)
  viewer._cesiumWidget._creditContainer.style.display = "none"; // 隐藏cesium icon
  // document.querySelector('.cesium-infoBox-camera').style.display = 'none';//隐藏infobox camera
  // 加载离线地图服务 - web服务影像提供器来实例化影像图层
  const imageryLayers = viewer.imageryLayers;
  const scene = viewer.scene;
  const provider = new Cesium.WebMapServiceImageryProvider({
    url: "http://localhost:8086/geoserver/ne/wms",
    layers: "ne:world",
    parameters: {
      format: "image/png",
      transparent: true,
      srs: "EPSG:4326"
    },
  });
  // imageryLayers.addImageryProvider(provider);
  // 要想加载贴地图形，就必须加载地形数据
  // 【贴地点】
  const myPoint = {
    id: 'myPoint',
    name: 'myPoint',
    show: true,
    position: Cesium.Cartesian3.fromDegrees(118, 32),
    point: {
      Color: Cesium.Color.RED,
      pixelSize: 30,
      heightReference: Cesium.HeightReference.CLAMP_TO_GROUND, // 贴地
      disableDepthTestDistance: Number.POSITIVE_INFINITY,
    }
  }
  viewer.entities.add(myPoint)

  // 【贴地线】
  const myPolyline = {
    id: 'myPolyline',
    name: 'myPolyline',
    show: true,
    polyline: {
      positions: Cesium.Cartesian3.fromDegreesArray([
        118, 30,
        119, 32,
        116, 35
      ]), // 线条起终
      width: 2, // 线条宽度
      material: Cesium.Color.DODGERBLUE.withAlpha(.8),
      clampToGround: true, // 是否贴地
    }
  }
  viewer.entities.add(myPolyline)
// 【贴地面】
  const myPolygon = {
    id: 'myPolygon',
    name: 'myPolygon',
    show: true,
    polygon: {
      hierarchy: Cesium.Cartesian3.fromDegreesArray([
        118, 30, 119, 32, 116, 32, 116, 30
      ]),
      outline: false,
      material: Cesium.Color.RED.withAlpha(.4),
      classificationType: Cesium.ClassificationType.BOTH, // BOTH - 贴地形也贴3D Tiles  TERRAIN - 仅贴合地形
    }
  }
  viewer.entities.add(myPolygon)

  // 【贴地矩形】
  const myRectangle = {
    id: 'myRectangle',
    name: 'myRectangle',
    show: true,
    rectangle: {
      coordinates: Cesium.Rectangle.fromDegrees(80.0, 30.0, 100.0, 35.0),
      material: Cesium.Color.BLUE.withAlpha(.8),
      classificationType: Cesium.ClassificationType.BOTH
    }
  }
  viewer.entities.add(myRectangle)

  // 【贴地椭圆】
  const myEllipse = {
    id: 'ellipse',
    name: '椭圆',
    show: true,
    position: Cesium.Cartesian3.fromDegrees(103.0, 40.0),
    ellipse: {
      semiMinorAxis: 250000.0,
      semiMajorAxis: 400000.0,
      material: Cesium.Color.YELLOW.withAlpha(.8),
      classificationType: Cesium.ClassificationType.BOTH
    }
  }
  viewer.entities.add(myEllipse)

  // 【贴地走廊】
  const corridor = {
    id: 'corridor',
    name: 'corridor',
    show: true,
    corridor: {
      positions: Cesium.Cartesian3.fromDegreesArray([
          100.0, 40.0,
          105.0, 40.0,
          105.0, 35.0,
      ]),
      width: 200000.0,
      material: Cesium.Color.YELLOW.withAlpha(.5),
      classificationType: Cesium.ClassificationType.BOTH
    }
  }
  viewer.entities.add(corridor)

  // 【贴地模型】
  let heading = Cesium.Math.toRadians(60); // 模型航向
  let pitch = 0; // 俯仰角
  let roll = 0; // 翻滚角
  let hpr = new Cesium.HeadingPitchRoll(heading, pitch, roll);
  const MyModel = {
    id: 'model',
    name: '小车模型',
    show: true,
    position: Cesium.Cartesian3.fromDegrees(118, 32), // 模型位置
    orientation: Cesium.Transforms.headingPitchRollQuaternion(
        Cesium.Cartesian3.fromDegrees(118, 32),
        hpr
    ), // 模型方向
    model: {
      uri: '/model/CesiumAir/Cesium_Air.glb',
      minimumPixelSize: 30, // 模型最小
      maximumPixelSize: 50000, // 模型最大
      scale: 3000,  // 当前比例
      heightReference: Cesium.HeightReference.CLAMP_TO_GROUND, // 贴地
      disableDepthTestDistance: Number.POSITIVE_INFINITY,
    }
  }
  viewer.entities.add(MyModel)
  viewer.flyTo(viewer.entities)

})
</script>

<style>
html,
body,
#cesiumContainer {
  width: 100%;
  height: 100%;
  margin: 0;
  padding: 0;
  overflow: hidden;
}
</style>
